//
//  ViewController.swift
//  SafeAreaInsetsDemo2
//
//  Created by 李煜 on 2024/6/6.
//https://blog.csdn.net/u013712343/article/details/107211700/

import UIKit

class ViewController: UIViewController {
    
    lazy var yellow: UIView  = {
        let y = UIView()
        y.backgroundColor = .yellow
        view.addSubview(y)
        return y
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .red
        //viewDidLoad获取safeAreaInset无效
//        debugPrint(view.safeAreaInsets,yellow.safeAreaInsets)
    }
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()

        //safeAreaInset: 子视图yellow在父视图red安全区域内,yellow.safeAreaInsets = .zero
        //yellow与父视图red有重叠,则yellow.safeAreaInsets指的是重叠的部分
        let y = 40.0
        yellow.frame = CGRect(x: 0, y: y, width: view.bounds.width, height: view.bounds.height - y)
        
        debugPrint(view.safeAreaInsets,yellow.safeAreaInsets)

    }

}

